package com.bootdo.loa.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.loa.domain.TbCommentDO;
import com.bootdo.loa.domain.TbMediaDO;
import com.bootdo.loa.service.TbCommentService;
import com.bootdo.loa.service.TbMediaService;
import com.bootdo.system.domain.CompanyPictureDO;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.DateUtils;
import com.bootdo.common.utils.OssUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.common.utils.Token;

/**
 * 
 * 
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2018-07-02 15:46:01
 */

@Controller
@RequestMapping("/loa/tbMedia")
public class TbMediaController {
	public static final String str = "/loa/tbMedia/toplay?url=";
	@Autowired
	private TbMediaService tbMediaService;
	@Autowired
	private TbCommentService tbCommentService;

	@GetMapping()
	@ResponseBody
	PageUtils TbMedia(@RequestParam Map<String, Object> params,Integer cateid) {
		if(cateid != null){
			params.put("cateid",cateid);
		}
		params.put("tags","mv");
		List<TbMediaDO> tbMediaList = tbMediaService.list(params);
		for (TbMediaDO tbMediaDO : tbMediaList) {
			tbMediaDO.setSrc(str+tbMediaDO.getSrc()+"&picture="+tbMediaDO.getPicture()+"&id="+tbMediaDO.getId());
			
		}
		Map<String, Object> params1 = new HashMap<>();
		params1.put("tags","pic");
		List<TbMediaDO> plist = tbMediaService.list(params1);
		
		PageUtils pageUtils = new PageUtils(tbMediaList,plist);
		return pageUtils;
	}
	
	
	//移动端视频中心
	@GetMapping("mobile")
	@ResponseBody
	PageUtils MTbMedia(@RequestParam Map<String, Object> params,Integer cateid) {
		if(cateid != null){
			params.put("cateid",cateid);
		}
		params.put("tags","mv");
		List<TbMediaDO> tbMediaList = tbMediaService.list(params);
		PageUtils pageUtils = new PageUtils(tbMediaList);
		return pageUtils;
	}
	
	
	
	
	@GetMapping("/index")
	String TbMedia1() {
		return "loa/tbMedia/index";
	}
	
	/**
	 * 微信分享遍历接口 
	 *
	 */
	@ResponseBody
	@GetMapping("/share")
	Map<String,Object> share(Integer id) {
		Map<String, Object> map = new HashMap<>();
		map.put("mediaId",id);
		TbMediaDO tbMediaDO = tbMediaService.get(id);
		List<TbCommentDO> list = tbCommentService.list(map);
		for (TbCommentDO tbCommentDO : list) {
			String timeBefore = DateUtils.getTimeBefore(tbCommentDO.getAddTime());
			tbCommentDO.setTimeBefore(timeBefore);
		}
		Map<String, Object> share = new HashMap<>();
		share.put("mv",tbMediaDO);
		share.put("list", list);
		return share;
	}
	
	
	/**
	 * 微信分享新增评论接口 
	 *
	 */
	@ResponseBody
	@GetMapping("/saveshare")
	R saveshare(Integer id,String content) {
		TbCommentDO comment = new TbCommentDO();
		comment.setMediaId(id);
		comment.setContent(content);
		comment.setAddTime(new Date());
		comment.setUserName("速用网友"+StringUtils.getOutTradeNo(6));
		int save = tbCommentService.save(comment);
		if(save == 1){
			return R.ok();
		}else {
			return R.error();
		}
		
	}


	@GetMapping("/toTbMedia")
	String toTbMedia(@RequestParam Map<String, Object> params, Model model) {

		return "loa/tbMedia/tbMedia";
	}

	@ResponseBody
	@GetMapping("/list")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		params.put("tags", "mv");
		Query query = new Query(params);
		List<TbMediaDO> tbMediaList = tbMediaService.list(query);
		int total = tbMediaService.count(query);
		PageUtils pageUtils = new PageUtils(tbMediaList, total);
		return pageUtils;
	}
	
	@GetMapping("/uploadpre")
	@ResponseBody
	List<TbMediaDO> uploadpre(@RequestParam Map<String, Object> params) {
		params.put("tags", "pic");
		Query query = new Query(params);
		List<TbMediaDO> tbMediaList = tbMediaService.list(query);
		return tbMediaList;
	}

	@GetMapping("/add")
	String add() {
		return "loa/tbMedia/add";
	}
	
	@GetMapping("/addlunbo")
	String addlunbo() {
		return "loa/tbMedia/addlunbo";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id, Model model) {
		TbMediaDO tbMedia = tbMediaService.get(id);
		model.addAttribute("tbMedia", tbMedia);
		return "loa/tbMedia/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public R save(TbMediaDO tbMedia) {
		if(StringUtils.isEmpty(tbMedia.getPicture())){
			return R.error("图片未上传");
		}
		tbMedia.setUptime(new Date());
		tbMedia.setTags("mv");//mv代表视频
		if (tbMediaService.save(tbMedia) > 0) {
			return R.ok();
		}
		return R.error();
	}
	
	
	/**
	 * 防止表单重复提交的DEMO
	 */
	@GetMapping("/adddemo")
	public String add(HttpServletRequest request) {
		Token.setToken(request);
		request.setAttribute("SesToken",request.getSession().getAttribute("SesToken"));
		return "loa/tbMedia/demo";
	}
	
	@PostMapping("/add")
	public void add(HttpServletRequest request,HttpServletResponse response){
		//验证重复提交的令牌
		if("false".equals(Token.validToken(request))) {
			System.out.println("重复提交！！");
			return;
			}
		try{
			System.out.println("提交成功！！");
		}catch(Exception e){
			e.printStackTrace();
			}
		}
	

	
	

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public R update(TbMediaDO tbMedia) {
		tbMediaService.update(tbMedia);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	public R remove(Integer id) {
		String imgUrl = tbMediaService.get(id).getPicture().substring(45, 62);
		if (tbMediaService.remove(id) > 0) {
			OssUtil.deleteBucketInfo(imgUrl);
			return R.ok();
		} else {
			return R.error();
		}
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	public R remove(@RequestParam("ids[]") Integer[] ids) {
		tbMediaService.batchRemove(ids);
		return R.ok();
	}

	/**
	 * 点击跳转
	 *
	 */
	@GetMapping("/toplay")
	public String toPlay(@RequestParam(value="url",required=false) String url,
			             @RequestParam(value="picture",required=false) String picture,
						 @RequestParam(value="id",required=false) Integer id,
						 Model model) {
		TbMediaDO tbMediaDO = tbMediaService.get(id);
		tbMediaDO.setBeforeTime(DateUtils.getTimeBefore(tbMediaDO.getUptime()));	
		model.addAttribute("media",tbMediaDO);
		model.addAttribute("url",url);
		model.addAttribute("picture",picture);
		return "loa/tbMedia/generic";
	}

}
